<template>
    <!--
        作者 luoyiming
        时间：2019-10-26
        描述：设置-上传设置
    -->
    <div class="product-add">
      <!--form表单-->
      <el-form size="small" ref="form" :model="form" label-width="200px">
        <!--文件上传设置-->
        <div class="common-form">文件上传设置</div>
        <el-form-item label="默认上传方式">
          <div>
            <el-radio v-model="form.radio" label="local" @change="getRadio">本地</el-radio>
            <!-- <el-radio v-model="form.radio" label="qiniu" @change="getRadio">七牛云存储</el-radio>
            <el-radio v-model="form.radio" label="aliyun" @change="getRadio">阿里云OSS</el-radio>
            <el-radio v-model="form.radio" label="qcloud" @change="getRadio">腾讯云COS</el-radio> -->
            <el-radio v-model="form.radio" label="minio" @change="getRadio">Minio存储</el-radio>
          </div>
        </el-form-item>
        <div v-if="form.radio == 'local'">
          <el-form-item label="空间域名 Domain">
            <el-input v-model="form.engine.local.domain" class="max-w460"></el-input>
            <div class="tips">请补全http:// 或 https://，例如：http://static.cloud.com</div>
          </el-form-item>
        </div>
        <!--七牛云存储-->
        <!-- <div v-if="form.radio == 'qiniu'">
          <el-form-item label="存储空间名称 Bucket"><el-input v-model="form.engine.qiniu.bucket" class="max-w460"></el-input></el-form-item>
          <el-form-item label="ACCESS_KEY AK"><el-input v-model="form.engine.qiniu.access_key" class="max-w460"></el-input></el-form-item>
          <el-form-item label="SECRET_KEY SK"><el-input v-model="form.engine.qiniu.secret_key" class="max-w460"></el-input></el-form-item>
          <el-form-item label="空间域名 Domain">
            <el-input v-model="form.engine.qiniu.domain" class="max-w460"></el-input>
            <div class="tips">请补全http:// 或 https://，例如：http://static.cloud.com</div>
          </el-form-item>
        </div> -->
        <!--阿里云OSS-->
        <!-- <div v-if="form.radio == 'aliyun'">
          <el-form-item label="存储空间名称 Bucket"><el-input v-model="form.engine.aliyun.bucket" class="max-w460"></el-input></el-form-item>
          <el-form-item label="AccessKeyId"><el-input v-model="form.engine.aliyun.access_key_id" class="max-w460"></el-input></el-form-item>
          <el-form-item label="AccessKeySecret"><el-input v-model="form.engine.aliyun.access_key_secret" class="max-w460"></el-input></el-form-item>
          <el-form-item label="空间域名 Domain">
            <el-input v-model="form.engine.aliyun.domain" class="max-w460"></el-input>
            <div class="tips">请补全http:// 或 https://，例如：http://static.cloud.com</div>
          </el-form-item>
        </div> -->
        <!--腾讯云COS-->
        <!-- <div v-if="form.radio == 'qcloud'">
          <el-form-item label="存储空间名称 Bucket"><el-input v-model="form.engine.qcloud.bucket"></el-input></el-form-item>
          <el-form-item label="所属地域 Region">
            <el-input v-model="form.engine.qcloud.region"></el-input>
            <div class="tips">请填写地域简称，例如：ap-beijing、ap-hongkong、eu-frankfurt</div>
          </el-form-item>
          <el-form-item label="SecretId"><el-input v-model="form.engine.qcloud.secret_id"></el-input></el-form-item>
          <el-form-item label="SecretKey"><el-input v-model="form.engine.qcloud.secret_key"></el-input></el-form-item>
          <el-form-item label="空间域名 Domain">
            <el-input v-model="form.engine.qcloud.domain"></el-input>
            <div class="tips">请补全http:// 或 https://，例如：http://static.cloud.com</div>
          </el-form-item>
        </div> -->
         <!--minio-->
        <div v-if="form.radio == 'minio'">
          <el-form-item label="存储空间名称 Bucket"><el-input v-model="form.engine.minio.bucket"></el-input></el-form-item>
          <el-form-item label="所属地域 Region">
            <el-input v-model="form.engine.minio.region"></el-input>
            <div class="tips">请填写地域简称，例如：ap-beijing、ap-hongkong、eu-frankfurt</div>
          </el-form-item>
          <el-form-item label="Key"><el-input v-model="form.engine.minio.key"></el-input></el-form-item>
          <el-form-item label="Secret"><el-input v-model="form.engine.minio.secret"></el-input></el-form-item>
          <el-form-item label="空间域名 Domain">
            <el-input v-model="form.engine.minio.domain"></el-input>
            <div class="tips">请补全http:// 或 https://，例如：http://static.cloud.com</div>
          </el-form-item>
          <el-form-item label="图片处理域名">
            <el-input v-model="form.engine.minio.style_domain"></el-input>
            <div class="tips">请补全http:// 或 https://，例如：http://static.cloud.com</div>
          </el-form-item>
        </div>
        <div class="draggable-list">
            <img class="item img-select" v-if="image!=''" @click="openUpload" :src="image" style="width: 100px;height: 100px;"/>
            <div v-else class="item img-select" @click="openUpload"><i class="el-icon-plus"></i></div>
        </div>
         <!--商品图片组件-->
        <Upload v-if="isUpload" :config="{ total: 9 }" :isupload="isUpload" @returnImgs="returnImgsFunc">上传图片</Upload>
        <!--提交-->
        <div class="common-button-wrapper"><el-button type="primary" @click="onSubmit" :loading="loading">提交</el-button></div>
      </el-form>
    </div>
  </template>
  
  <script>
  import SysConfigApi from '@/api/sysconfig.js';
  import Upload from '@/components/file/Upload';
  export default {
    components: {
        Upload,
    },
    data() {
      return {
        /*切换菜单*/
        // activeIndex: '1',
        /*form表单数据*/
        isUpload:false,
        image:'',
        config:{},
        form: {
          radio: 'local',
          engine: {
            local:{
                domain:''
            },
            // qiniu: {
            //   // domain: '',
            //   // access_key: '',
            //   // secret_key: '',
            //   // bucket: '',
            // },
            // aliyun: {
            //   // domain: '',
            //   // access_key_id: '',
            //   // access_key_secret: '',
            //   // bucket: '',
            // },
            // qcloud: {
            //   // domain: '',
            //   // region: '',
            //   // secret_id: '',
            //   // secret_key: '',
            //   // bucket: '',
            // },
            minio:{}
          }
        },
        loading: false
      };
    },
    created() {
      this.getData();
    },
  
    methods: {
      getData() {
        let self = this;
        SysConfigApi.upload({}, true)
          .then(data => {
            self.config = data.data;
            let vars = JSON.parse(data.data.configValue);
            self.form.radio = vars.default;
            // self.form.engine.qiniu = vars.engine.qiniu;
            // self.form.engine.aliyun = vars.engine.aliyun;
            // self.form.engine.qcloud = vars.engine.qcloud;
            self.form.engine.local = vars.engine.local;
            self.form.engine.minio = vars.engine.minio;
            console.log(self.form)
          })
          .catch(error => {
            console.log(error);
          });
      },
      //提交表单
      onSubmit() {
        let self = this;
        self.loading = true;
        let params = this.config;
        let storeConfig={
            default: self.form.radio,
            engine:self.form.engine
        }
        params.configValue = JSON.stringify(storeConfig);
        SysConfigApi.updateConfig(params, true)
          .then(data => {
            self.loading = false;
            self.$message({
              message: '恭喜你，上传设置成功',
              type: 'success'
            });
            // self.$router.push('/setting/Storage');
          })
          .catch(error => {
            self.loading = false;
          });
      },
      //监听单选按钮
      getRadio(val) {},
      openUpload(){
        this.isUpload = true;
      },
      returnImgsFunc(e){
        console.log(e)
        if (e != null) {
            this.$set(this, 'image', e[0].file_path);
        }
        this.isUpload = false;
      }
    }
  };
  </script>
  
  <style>
  .tips {
    color: #ccc;
  }
  .draggable-list {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
.draggable-list .wrapper > span {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}

.draggable-list .item {
  position: relative;
  width: 110px;
  height: 110px;
  margin-top: 10px;
  margin-right: 10px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #dddddd;
}
.draggable-list .item:hover .delete-btn {
  display: block;
}

.draggable-list .item img {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  max-height: 100%;
  max-width: 100%;
}

.draggable-list .img-select {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px dashed #dddddd;
  font-size: 30px;
}

.draggable-list .img-select i {
  color: #409eff;
}
  </style>
  